package com.jung.customer.dao.impl;

import java.util.List;

import com.jung.common.HibernateEntityManagerImpl;
import com.jung.customer.dao.CustomerHibernateDao;
import com.jung.customer.model.Customer;
import com.jung.exception.SkeletonException;
import com.jung.exception.SkeletonSystemException;

public class CustomerHibernateDaoImpl extends HibernateEntityManagerImpl<Customer> implements CustomerHibernateDao {

	public boolean addCustomer(Customer customer) {
		try {
			super.save(customer);
			return true;
		} catch (SkeletonSystemException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean deleteCustomer(Customer customer) {
		try {
			super.remove(customer);
			return true;
		} catch (SkeletonSystemException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean updateCustomer(Customer customer) {
		try {
			super.update(customer);
			return true;
		} catch (SkeletonSystemException e) {
			e.printStackTrace();
		}
		return false;
	}

	public Customer getCustomerById(int id) {
		String hql="from Customer where id="+id;
		Object object = null;
		try {
			List customerList =super.executeHql(hql); 
			if(customerList.size()<1){
				return null;
			}
			object = customerList.get(0);
		} catch (SkeletonException e) {
			e.printStackTrace();
		}
		Customer customer = (Customer)object;
		return customer;
		//return super.findById(Customer.class, id);
	}

	public Customer getCustomerByName(String userName){
		String hql="from Customer where userName='"+userName+"'";
		Object object = null;
		try {
			List customerList =super.executeHql(hql); 
			if(customerList.size()<1){
				return null;
			}
			object = customerList.get(0);
		} catch (SkeletonException e) {
			e.printStackTrace();
		}
		Customer customer = (Customer)object;
		return customer;
	}

	public List<Customer> getAll() {
		String hql="from Customer";
		List<Customer> customerList = null;
		Object object=null;
		try {
			//customerList = super.findAll(Customer.class);
			object=super.executeHql(hql);
			customerList=(List<Customer>)object;
		} catch (SkeletonException e) {
			e.printStackTrace();
		}
		return customerList;
	}

	

}
